$(function() {
	var danmei = $('#chooseone>li');
	//关闭魔态窗
	$('.close').click(function() {
	});
	//循环获取item
	$.each(danmei, function() {
		var index;
		var that = this;
		$(this).hover(function() {
			var data_id = $(that).data('id');
			var arr = new Array();
			arr = data_id.split('-');


			index = layer.tips(arr[0] + '号金像,剩余' + arr[1] + '枚 '+arr[2]+'元/枚', $(this), {
				tips: [1, '#be0900'],
				time: 4000
			})

		}, function() {
			layer.close(index);
		});
	});
	$(document).on('click', '#preparechoose li', function() {
		$(this).addClass('active').siblings('li').removeClass('active');
		var data_id = $(this).data('id');
		var data_price = $(this).data('price');
		var arr = [];
		arr = data_id.split('-');
		$('#haschoose li').each(function() {
			var data_id2 = $(this).data('id');
			var arr2 = [];
			var that = this;
			arr2 = data_id2.split('-');
			if (arr[0] == arr2[0]) {
				$(that).remove();
			}
		})
		var html = '<li data-id="' + data_id + '" data-price="' + data_price + '">' + $(this).html() + '<p></p></li>';
		$('#haschoose').append(html);

	})

	$('#modal_close, #jxqgm').on('click', function() {
		$('#haschoose li').each(function() {
			$(this).clone().appendTo($('#onchoose'));
		})

		var price = 0;
		$('#haschoose li').each(function(){
			price += parseInt($(this).data('price'));
		})

		$('#price').html(price);

		$('#haschoose').empty();

		$('#shadow').hide(200);
		$('body').removeClass('bodysc');
	})

	window.viptype = $('#level').data('id');
	function setuserinfo(level){
		var info = {};
		if(level == 1){
			info.buy = false;
			info.name = '400/地级会员';
		}else if(level == 2){
			info.buy = 20;
			info.name = '区县代表';
		}else if(level == 3){
			info.buy = 10;
			info.name = '普通会员';
		}else if(level == 4){
			info.buy = 10;
			info.name = '非会员';
		}
		return info;
	}
	var theinfo = setuserinfo(window.viptype);
	$('#huiyuanjibie').html(theinfo.name);
	$('#zonggong').html(theinfo.buy);
	//alert(theinfo.buy);
	$('#shengyugoumai').html(theinfo.buy);

	function checkviptype(level){
		var much;
		if(level == 1){
			much = false;
		}else if(level == 2){
			much = 20;
		}else if(level == 3 || level == 4){
			much = 10;
		}
		return much;
	}
	function howmany(much){
		var alreadychoose =  $('#onchoose li').length;
		var lis = true;
		if(!much){
			layer.alert('400/地级会员不能购买单枚金像，您可以去购买全套。',{
				title : '提示信息'
				},function(){
                    location.href="/Home/list";
			})
			lis = false;
		}else if(alreadychoose >= much){
			layer.alert('您的会员级别只能购买' + much + '枚金像',{
				title : '提示信息'
			});
			lis = false;
		}
		return lis;
	}

	$(document).on('click', '#chooseone li', function() {
		if(window.islogin == 0){
			layer.alert('请您登录',{
				title : '提示信息'
			},function(index){
				$('#loginbtn').click();
				layer.close(index);
			});
			return false;
		}else{

			var howmuch = checkviptype(window.viptype);
			var canbuy = howmany(howmuch);
			if(!canbuy){
				return false;
			}

			var nowlength = $('#onchoose li').length;
			var lastlength = theinfo.buy - nowlength;
			$('#shengyugoumai').html(lastlength);
			if(lastlength <= 0){
				layer.alert('您已达到购买数量上限',{
					title : '提示信息'
				});
				return false;
			}

			var type_num  = $(this).data('type');
			var index;
			$.ajax({
				type : 'post',
				url : '/single/percoinAjax',
				data : {type_num:type_num},
				beforeSend : function(){
					index = layer.load(2);
				},
				complete : function(){
					layer.close(index);
				},
				success : function(data){
					//var datajson = $.parseJSON(data);
					//console.log(data);
					if(data == '' || data == [] || !data){
						layer.alert('暂无数据！');
						return false;
					}
					
					for(var a = 0; a < data[0]['number'].length; a++){
						if(type_num == data[0]['number'][a]['type_num']){
							layer.alert('您已购买过'+type_num+'号金像，请选购其它号码的金像',{
								title : '提示信息'
							})
							return false;
						}
					}
					//alert(data[0]['order']);
					var front_src = data[0]['photo_front'];
					var back_src = data[0]['photo_negative'];
					var front_title = data[0]['mean_front_title'];
					var back_title = data[0]['mean_back_title'];

					function getarr(title){
						var arr = [];
						arr = title.split('——');
						return arr;
					}

					var front_title1 = getarr(front_title)[0];
					var front_title2 = '——' + getarr(front_title)[1];
					var back_title1 = getarr(back_title)[0];
					var back_title2 = '——' + getarr(back_title)[1];

					$('#front_img').attr('src', front_src);
					$('#back_img').attr('src', back_src);
					$('#front .front').html(front_title1);
					$('#front .back').html(front_title2);
					$('#back .front').html(back_title1);
					$('#back .back').html(back_title2);

					var ind1, ind2 ,ind3 ,ind4 ,ind5 ,ind6 ,ind7 ,ind8 ,ind9 ,ind10;

					var str1 ,str2 ,str3 ,str4 ,str5 ,str6 ,str7 ,str8 ,str9 ,str10;
					var datalength = data.length;
					var singlelength = datalength/4;
					//console.log( '每个区间'+singlelength);

					//var datajson = $.parseJSON(data);
					$.each(data, function(index, elem){
						//if(index)
						if( data[index]['status'] == 0){
							if(index < 100){
								str1 += '<li data-id="' + type_num + '-' + data[index]['group_num'] + '" data-price="' + data[index]['price'] + '"><div class="item"><span class="numt">' + type_num + '</span><i>★★</i>' + data[index]['group_num'] + '</div></li>';
							}else if(index < 200){
								str2 += '<li data-id="' + type_num + '-' + data[index]['group_num'] + '" data-price="' + data[index]['price'] + '"><div class="item"><span class="numt">' + type_num + '</span><i>★★</i>' + data[index]['group_num'] + '</div></li>';
							}else if(index < 300){
								str3 += '<li data-id="' + type_num + '-' + data[index]['group_num'] + '" data-price="' + data[index]['price'] + '"><div class="item"><span class="numt">' + type_num + '</span><i>★★</i>' + data[index]['group_num'] + '</div></li>';
							}else if(index < 400){
								str4 += '<li data-id="' + type_num + '-' + data[index]['group_num'] + '" data-price="' + data[index]['price'] + '"><div class="item"><span class="numt">' + type_num + '</span><i>★★</i>' + data[index]['group_num'] + '</div></li>';
							}else if(index < 500){
								str5 += '<li data-id="' + type_num + '-' + data[index]['group_num'] + '" data-price="' + data[index]['price'] + '"><div class="item"><span class="numt">' + type_num + '</span><i>★★</i>' + data[index]['group_num'] + '</div></li>';
							}else if(index < 600){
								str6 += '<li data-id="' + type_num + '-' + data[index]['group_num'] + '" data-price="' + data[index]['price'] + '"><div class="item"><span class="numt">' + type_num + '</span><i>★★</i>' + data[index]['group_num'] + '</div></li>';
							}else if(index < 700){
								str7 += '<li data-id="' + type_num + '-' + data[index]['group_num'] + '" data-price="' + data[index]['price'] + '"><div class="item"><span class="numt">' + type_num + '</span><i>★★</i>' + data[index]['group_num'] + '</div></li>';
							}else if(index < 800){
								str8 += '<li data-id="' + type_num + '-' + data[index]['group_num'] + '" data-price="' + data[index]['price'] + '"><div class="item"><span class="numt">' + type_num + '</span><i>★★</i>' + data[index]['group_num'] + '</div></li>';
							}else if(index < 900){
								str9 += '<li data-id="' + type_num + '-' + data[index]['group_num'] + '" data-price="' + data[index]['price'] + '"><div class="item"><span class="numt">' + type_num + '</span><i>★★</i>' + data[index]['group_num'] + '</div></li>';
							}else if(index < 1000){
								str10 += '<li data-id="' + type_num + '-' + data[index]['group_num'] + '" data-price="' + data[index]['price'] + '"><div class="item"><span class="numt">' + type_num + '</span><i>★★</i>' + data[index]['group_num'] + '</div></li>';
							}
						}else{
							if(index < 100){
								str1 += '<li style="display:none;" data-id="' + type_num + '-' + data[index]['group_num'] + '" data-price="' + data[index]['price'] + '"><div class="item"><span class="numt">' + type_num + '</span><i>★★</i>' + data[index]['group_num'] + '</div></li>';
							}else if(index < 200){
								str2 += '<li style="display:none;" data-id="' + type_num + '-' + data[index]['group_num'] + '" data-price="' + data[index]['price'] + '"><div class="item"><span class="numt">' + type_num + '</span><i>★★</i>' + data[index]['group_num'] + '</div></li>';
							}else if(index < 300){
								str3 += '<li style="display:none;" data-id="' + type_num + '-' + data[index]['group_num'] + '" data-price="' + data[index]['price'] + '"><div class="item"><span class="numt">' + type_num + '</span><i>★★</i>' + data[index]['group_num'] + '</div></li>';
							}else if(index < 400){
								str4 += '<li style="display:none;" data-id="' + type_num + '-' + data[index]['group_num'] + '" data-price="' + data[index]['price'] + '"><div class="item"><span class="numt">' + type_num + '</span><i>★★</i>' + data[index]['group_num'] + '</div></li>';
							}else if(index < 500){
								str5 += '<li style="display:none;" data-id="' + type_num + '-' + data[index]['group_num'] + '" data-price="' + data[index]['price'] + '"><div class="item"><span class="numt">' + type_num + '</span><i>★★</i>' + data[index]['group_num'] + '</div></li>';
							}else if(index < 600){
								str6 += '<li style="display:none;" data-id="' + type_num + '-' + data[index]['group_num'] + '" data-price="' + data[index]['price'] + '"><div class="item"><span class="numt">' + type_num + '</span><i>★★</i>' + data[index]['group_num'] + '</div></li>';
							}else if(index < 700){
								str7 += '<li style="display:none;" data-id="' + type_num + '-' + data[index]['group_num'] + '" data-price="' + data[index]['price'] + '"><div class="item"><span class="numt">' + type_num + '</span><i>★★</i>' + data[index]['group_num'] + '</div></li>';
							}else if(index < 800){
								str8 += '<li style="display:none;" data-id="' + type_num + '-' + data[index]['group_num'] + '" data-price="' + data[index]['price'] + '"><div class="item"><span class="numt">' + type_num + '</span><i>★★</i>' + data[index]['group_num'] + '</div></li>';
							}else if(index < 900){
								str9 += '<li style="display:none;" data-id="' + type_num + '-' + data[index]['group_num'] + '" data-price="' + data[index]['price'] + '"><div class="item"><span class="numt">' + type_num + '</span><i>★★</i>' + data[index]['group_num'] + '</div></li>';
							}else if(index < 1000){
								str10 += '<li style="display:none;" data-id="' + type_num + '-' + data[index]['group_num'] + '" data-price="' + data[index]['price'] + '"><div class="item"><span class="numt">' + type_num + '</span><i>★★</i>' + data[index]['group_num'] + '</div></li>';
							}
						}

						if(index == 99){
							ind1 = parseInt(data[index]['group_num'] - 99 ) + '~' + data[index]['group_num'];
						}else if(index == 199){
							ind2 = parseInt(data[index]['group_num'] - 99 ) + '~' + data[index]['group_num'];
						}else if(index == 299){
							ind3 = parseInt(data[index]['group_num'] - 99 ) + '~' + data[index]['group_num'];
						}else if(index == 399){
							ind4 = parseInt(data[index]['group_num'] - 99 ) + '~' + data[index]['group_num'];
						}else if(index == 499){
							ind5 = parseInt(data[index]['group_num'] - 99 ) + '~' + data[index]['group_num'];
						}else if(index == 599){
							ind6 = parseInt(data[index]['group_num'] - 99 ) + '~' + data[index]['group_num'];
						}else if(index == 699){
							ind7 = parseInt(data[index]['group_num'] - 99 ) + '~' + data[index]['group_num'];
						}else if(index == 799){
							ind8 = parseInt(data[index]['group_num'] - 99 ) + '~' + data[index]['group_num'];
						}else if(index == 899){
							ind9 = parseInt(data[index]['group_num'] - 99 ) + '~' + data[index]['group_num'];
						}else if(index == 999){
							ind10 = parseInt(data[index]['group_num'] - 99 ) + '~' + data[index]['group_num'];
						}

					});
					$('#preparechoose div ul').empty();
					$('#preparechoose div ul:eq(0)').append(str1);
					$('#preparechoose div ul:eq(1)').append(str2);
					$('#preparechoose div ul:eq(2)').append(str3);
					$('#preparechoose div ul:eq(3)').append(str4);
					$('#preparechoose div ul:eq(4)').append(str5);
					$('#preparechoose div ul:eq(5)').append(str6);
					$('#preparechoose div ul:eq(6)').append(str7);
					$('#preparechoose div ul:eq(7)').append(str8);
					$('#preparechoose div ul:eq(8)').append(str9);
					$('#preparechoose div ul:eq(9)').append(str10);
					$('#choosetit .t:eq(0)').html(ind1);
					$('#choosetit .t:eq(1)').html(ind2);
					$('#choosetit .t:eq(2)').html(ind3);
					$('#choosetit .t:eq(3)').html(ind4);
					$('#choosetit .t:eq(4)').html(ind5);
					$('#choosetit .t:eq(5)').html(ind6);
					$('#choosetit .t:eq(6)').html(ind7);
					$('#choosetit .t:eq(7)').html(ind8);
					$('#choosetit .t:eq(8)').html(ind9);
					$('#choosetit .t:eq(9)').html(ind10);

					$('#shadow').fadeIn(200);
					//魔态窗显示禁止显示滚动条
					$('body').addClass('bodysc');


			//alert(type_num);
			$('#onchoose li').each(function() {
				$(this).clone().appendTo($('#haschoose'));
			})
			if ($('#onchoose li').length == 0) {
				//console.log($('#onchoose li').length);
				$('#preparechoose li').removeClass('active');
			}

			$('#onchoose').empty();

			//这个each在ajax之后
			$('#haschoose li').each(function() {
				var data_id = $(this).data('id');
				var that = this;
				$('#preparechoose li').each(function() {
					var data_id2 = $(this).data('id');
					if (data_id == data_id2) {
						$(that).addClass('active');
					}
				})
			})


					console.log('success');
				},
				error : function(err){
					console.log('error');
				}
			});
		}
	})

	$(document).on('click', '#haschoose li>p', function() {
		$(this).parent('li').remove();
		$('#preparechoose li').removeClass('active');
	})

	$(document).on('click', '#onchoose li>p', function(event) {
		var data_id = $(this).parent('li').data('id');
		$(this).parent('li').remove();
		var price = 0;
		$('#onchoose li').each(function(){
			price += parseInt($(this).data('price'));
		})

		$('#price').html(price);
		event.stopPropagation();
	})

	$('#choosetit div.t').tabHoverDelay('#preparechoose',{
		tabTitClass : 'active',
		tabConClass : 'choosecon'
	});


	$('#help1, #help2').click(function(){
		$('#shadow2').fadeIn(200);
		//模态框显示禁止显示滚动条
		$('body').addClass('bodysc');
		return false;
	})

	$('#backto,#guize_close').click(function(){
		$('#shadow2').hide(200);
		$('body').removeClass('bodysc');
	})

});